package com.itfitness.filter;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import com.itfitness.filter.widget.camera.CameraHelper;
import jp.co.cyberagent.android.gpuimage.GPUImage;
import jp.co.cyberagent.android.gpuimage.GPUImageDissolveBlendFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageSketchFilter;
import jp.co.cyberagent.android.gpuimage.GPUImageThresholdEdgeDetection;
import jp.co.cyberagent.android.gpuimage.GPUImageToonFilter;

public class MainActivity extends AppCompatActivity{
    static {
        System.loadLibrary("native-lib");
    }
    private CameraHelper cameraHelper;
    private Bitmap bitmap;
    private SurfaceView svFilter;
    private SurfaceHolder mHolder;
    private Canvas mCanvas;
    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Matrix matrix;
    private GPUImage gpuImage;
    private int opera = 0;
    private Button bt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initDatas();
    }

    private void initDatas() {
        mHolder = svFilter.getHolder();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        cameraHelper.stopCamera();
    }

    private void initView(){
        gpuImage = new GPUImage(this);
        gpuImage.setFilter(new GPUImageDissolveBlendFilter());
        svFilter = (SurfaceView) findViewById(R.id.sv_filter);
        bt = (Button) findViewById(R.id.bt);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                opera++;
                if(opera > 3){
                    opera = 0;
                }
                switch (opera){
                    case 0:
                        gpuImage.setFilter(new GPUImageDissolveBlendFilter());
                        break;
                    case 1:
                        //素描
                        gpuImage.setFilter(new GPUImageSketchFilter());
                        break;
                    case 2:
                        //水彩
                        gpuImage.setFilter(new GPUImageToonFilter());
                        break;
                    case 3:
                        //二值化
                        gpuImage.setFilter(new GPUImageThresholdEdgeDetection());
                        break;
                }
            }
        });
        cameraHelper = new CameraHelper(this);
        cameraHelper.startCamera();
        cameraHelper.setFrameCallback(new CameraHelper.FrameCallback() {
            @Override
            public void onDecodeFrame(byte[] data,final Camera camera) {
                try {
                    mCanvas=mHolder.lockCanvas();
                    //初始化画布并在画布上画一些东西
                    int width = camera.getParameters().getPreviewSize().width;
                    int height = camera.getParameters().getPreviewSize().height;
                    int[] result = yuv2Rgb(data,width,height);
                    if(matrix == null){
                        matrix = new Matrix();
                        matrix.preScale(mCanvas.getWidth()/1.0f/height,mCanvas.getHeight()/1.0f/width);
                    }
                    Bitmap bitmapTemp = Bitmap.createBitmap(result, height, width, Bitmap.Config.RGB_565);
                    bitmapTemp = Bitmap.createBitmap(bitmapTemp, 0,0,height,width, matrix, false);
                    gpuImage.setImage(bitmapTemp);
                    bitmap = gpuImage.getBitmapWithFilterApplied();
                    mCanvas.drawBitmap(bitmap,0,0,paint);
                    bitmapTemp.recycle();
                }catch (Exception e){
                }finally {
                    //判断画布是否为空，从而避免黑屏情况
                    if(mCanvas!=null){
                        mHolder.unlockCanvasAndPost(mCanvas);
                    }
                    camera.addCallbackBuffer(data);
                }
            }
        });
    }
    public native int[] yuv2Rgb(byte[] data,int width,int height);
}
